package com.gxuwz.leaveSystem.dao;

import com.gxuwz.leaveSystem.entity.Course;
import com.gxuwz.leaveSystem.entity.Department;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;

public class CourseDao {

    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 添加
     * @param course
     * @return
     */
    public int add(Course course){
        String sql = "insert into sys_course(courseID, classID, courseName, year, term, hour) value(?,?,?,?,?,?)";
        Object[] obj = new Object[] {
                course.getCourseID(),
                course.getClassID(),
                course.getCourseName(),
                course.getYear(),
                course.getTerm(),
                course.getHour()
        };
        int num = this.jdbcTemplate.update(sql,obj);
        return num;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(String id){
        String sql = "delete from `leave`.sys_course where courseID=?";
        int num = this.jdbcTemplate.update(sql,id);
        return num;
    }

    /**
     * 修改
     * @param course
     * @return
     */

    public int update(Course course){
        String sql = "update sys_course set classID = ?,courseName = ?,year = ?,term = ?,hour = ? where courseID = ?";
        Object[] up = new Object[] {
                course.getClassID(),
                course.getCourseName(),
                course.getYear(),
                course.getTerm(),
                course.getHour(),
                course.getCourseID()
        };
        int num = this.jdbcTemplate.update(sql,up);
        return num;
    }

    /**
     * 根据id查列表
     * @param id
     * @return
     */

    public Course findByid(String id){
        String sql = "select * from sys_course where courseID = ?";
        RowMapper<Course > rowMapper = new BeanPropertyRowMapper<Course>(Course.class);
        List<Course> courses = this.jdbcTemplate.query(sql, rowMapper, id);
        if (courses.isEmpty()) {
            return null; // 或者返回一个默认的User对象
        } else {
            return courses.get(0);
        }
    }

    /**
     * 查询全部
     * @return
     */
    public List<Course> findAll(){
        String sql = "select * from sys_course";
        RowMapper<Course > rowMapper = new BeanPropertyRowMapper<Course>(Course .class);
        return  this.jdbcTemplate.query(sql,rowMapper);
    }

    /**
     * 根据关键字搜索
     * @param keyword
     * @return
     */
    public List<Course > search(String keyword) {
        String sql = "SELECT * FROM sys_course WHERE sys_course.courseID LIKE ? OR sys_course.classID LIKE ? OR sys_course.courseName LIKE ? OR sys_course.year LIKE ? OR sys_course.term LIKE ? OR sys_course.hour LIKE ?";
        return jdbcTemplate.query(sql, new Object[]{"%" + keyword + "%","%" + keyword + "%","%" + keyword + "%","%" + keyword + "%","%" + keyword + "%","%" + keyword + "%"}, new BeanPropertyRowMapper<>(Course.class));
    }
}
